<?php

namespace App\Http\Controllers;

use Illuminate\Contracts\Validation\Validator as ValidationValidator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Validator as FacadesValidator;
use Symfony\Component\HttpFoundation\UrlHelper;

class BLWAPIController extends Controller
{
    public function test(){
        $config=self::getConfig();
        $url="http://api.polyv.net/live/v3/user/get-info";
        $timestamp=self::getTimestamp();
        $params=[
            "appId"=>$config['app_id'],
            "timestamp"=>$timestamp
        ];
        $sign=self::makeSignData($config['app_secret'],$params);
        $response=Http::get($url,array_merge($params,['sign'=>$sign]));
        $body=$response->body();
        return $body;
    }
    public function modifyChannelName(Request $request){
        $rule=[
            "name"=>"required|max:255",
            "channelid"=>"required|integer"
            ];
            $ruleMsg= [
        'name.required' => 'A name is required',
        'name.max' => 'A title is max length is 255',
        'channelid.required' => 'A channelid is required',
        'channelid.integer' => 'A channelid is integer',
            ];
            $validate=FacadesValidator::make([
                "name"=>$request['name'],
                "channelid"=>$request['channelid']
            ],$rule,$ruleMsg);
            if($validate->fails()){
                return $validate->errors();
            }
            $body=$validate->validated();
        $config=self::getConfig();
        $name=$body['name'];
        $channelId=$body['channelid'];
        $url="http://api.polyv.net/live/v2/channels/%s/update";
        $url=sprintf($url,$channelId);
        $timestamp=self::getTimestamp();
        $params=[
            "appId"=>$config['app_id'],
            "timestamp"=>$timestamp,
            "name"=>$name
        ];
        $sign=self::makeSignData($config['app_secret'],$params);
        $data=array_merge($params,['sign'=>$sign]);
        $response=Http::asForm()->post($url,$data);
        $body=$response->body();
        return $body;
    }
    public static function newChannel($params){
        $config=self::getConfig();
        $channelpwd=$params['channelpwd']??null;
        $seminarhostpwd=$params['seminarhostpwd']??null;
        $seminarattendeepwd=$params['seminarattendeepwd']??null;
        $title=$params['title']??date('YmdHis').'直播';
        $scene=$params['scene']??'alone';
        $template=$params['template']??'portrait_alone';
        $url="http://api.polyv.net/live/v4/channel/create";
        $body=[
            'name'=>$title,
            'newScene'=>$scene,
            'template'=>$template
        ];
        if(!empty($channelpwd)){
            $body['channelPasswd']=$channelpwd;
        }
        if(!empty($seminarhostpwd)){
            $body['seminarHostPassword']=$seminarhostpwd;
        }
        if(!empty($seminarattendeepwd)){
            $body['seminarAttendeePassword']=$seminarattendeepwd;
        }
        $timestamp=self::getTimestamp();
        $signParams=[
            "appId"=>$config['app_id'],
            "timestamp"=>$timestamp
        ];
        $sign=self::makeSignData($config['app_secret'],$signParams);
        $data=array_merge($signParams,['sign'=>$sign]);
        $url=$url.'?'.http_build_query($data);
        echo $url;
        $response=Http::post($url,$body);
        $body=$response->body();
        return $body;
    }
    public static function removeChannel($params){
        $config=self::getConfig();
        $channelId=$params['channelId']??null;
        $userId=$params['userId']??null;
        if(empty($channelId)||empty($userId))
         return null;

        $url="http://api.polyv.net/live/v2/channels/%s/delete";
        $url=sprintf($url,$channelId);
        echo $url.PHP_EOL;
        $timestamp=self::getTimestamp();
        $params=[
            "appId"=>$config['app_id'],
            "timestamp"=>$timestamp,
            "userId"=>$userId
        ];
        echo json_encode($params).PHP_EOL;
        $sign=self::makeSignData($config['app_secret'],$params);
        $data=array_merge($params,['sign'=>$sign]);
        $response=Http::asForm()->post($url,$data);
        $body=$response->body();
        return $body;
    }
    //
    public static function getConfig(){
        return [
            "user_id"=>env('BLW_USER_ID'),
            "app_id"=>env('BLW_APP_ID'),
            "app_secret"=>env('BLW_APP_SECRET')
        ];
    }
    public static function getTimestamp(){
        $timestamp=microtime();
        [$msec,$sec]=explode(' ',$timestamp);
        $msec*=1000;
        return intval(intval($sec).''.round($msec));
    }
    public static function makeSignData($secert,$data){
        ksort($data);
        $s="";
        foreach($data as $k=>$v){
            $s.=$k.$v;
        }
        $s=$secert.$s.$secert;
        return strtoupper(md5($s));
    }
}
